﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SLWebApp
{
    public partial class MonthSchedule : UserControl
    {
        ServiceReference_HB.ServiceForSLClient clientService = new ServiceReference_HB.ServiceForSLClient();
        string userid = string.Empty;

        public MonthSchedule()
        {
            InitializeComponent();

            System.Windows.Browser.HtmlDocument doc = System.Windows.Browser.HtmlPage.Document;
            if (doc.QueryString.Count > 0)
            {
                userid = doc.QueryString["userid"].ToString();
            }
            Init();
        }

        //数据初始化
        private void Init()
        {
            if (!string.IsNullOrEmpty(userid))
            {
                //获取登陆人的名字
                int intID = 0;
                int.TryParse(userid, out intID);
                clientService.GetCustomerNameAsync(intID);
                clientService.GetCustomerNameCompleted += new EventHandler<ServiceReference_HB.GetCustomerNameCompletedEventArgs>(clientService_GetCustomerNameCompleted);
                //获取大航线名字
                if (intID == 1)
                    clientService.GetMainSeaRoutes_ByUserIdAsync(intID, "1");
                else
                    clientService.GetMainSeaRoutes_ByUserIdAsync(intID, "0");
                clientService.GetMainSeaRoutes_ByUserIdCompleted += new EventHandler<ServiceReference_HB.GetMainSeaRoutes_ByUserIdCompletedEventArgs>(clientService_GetMainSeaRoutes_ByUserIdCompleted);
            }
        }

        void clientService_GetMainSeaRoutes_ByUserIdCompleted(object sender, ServiceReference_HB.GetMainSeaRoutes_ByUserIdCompletedEventArgs e)
        {
            ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
            comboxSeaRoutes.ItemsSource = col;
        }

        void clientService_GetCustomerNameCompleted(object sender, ServiceReference_HB.GetCustomerNameCompletedEventArgs e)
        {
            txtAgentName.Text = e.Result;
        }

        private void comboxSeaRoutes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                if (!string.IsNullOrEmpty(userid))
                {
                    //获取登陆人的名字
                    int intID = 0;
                    int.TryParse(userid, out intID);
                    ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                    
                    //绑定服务航线的下拉框
                    if (intID == 1)
                        clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "1", item.ID);
                    else
                        clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "0", item.ID);
                    clientService.GetChildSeaRoutes_ByLineIDCompleted += new EventHandler<ServiceReference_HB.GetChildSeaRoutes_ByLineIDCompletedEventArgs>(clientService_GetChildSeaRoutes_ByLineIDCompleted);
                    //获取班期
                }
            }
        }

        void clientService_GetChildSeaRoutes_ByLineIDCompleted(object sender, ServiceReference_HB.GetChildSeaRoutes_ByLineIDCompletedEventArgs e)
        {
            ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
            comboxServiceLine.ItemsSource = col;
        }

        private void comboxServiceLine_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                if (!string.IsNullOrEmpty(userid))
                {
                    //获取登陆人的名字
                    int intID = 0;
                    int.TryParse(userid, out intID);
                    ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                    clientService.GetVesselDateAsync(intID, item.ID.ToString());
                    clientService.GetVesselDateCompleted += new EventHandler<ServiceReference_HB.GetVesselDateCompletedEventArgs>(clientService_GetVesselDateCompleted);
                    //显示预分码头.
                    clientService.GetTermailAsync(item.ID);
                    clientService.GetTermailCompleted += new EventHandler<ServiceReference_HB.GetTermailCompletedEventArgs>(clientService_GetTermailCompleted);
                    //显示船公司名称。
                    clientService.GetVesselNameAsync(item.ID);
                    clientService.GetVesselNameCompleted += new EventHandler<ServiceReference_HB.GetVesselNameCompletedEventArgs>(clientService_GetVesselNameCompleted);
                }
            }
        }

        void clientService_GetVesselNameCompleted(object sender, ServiceReference_HB.GetVesselNameCompletedEventArgs e)
        {
            txtVesselName.Text = e.Result;
        }

        void clientService_GetTermailCompleted(object sender, ServiceReference_HB.GetTermailCompletedEventArgs e)
        {
            ObservableCollection<ServiceReference_HB.MainSeaRoutes> col=e.Result;
            if (col.Count >= 1)
            {
                this.txtTerminal.Text = col[0].RouteName;
                this.txtTerminal.Tag = col[0].ID;
            }
        }

        void clientService_GetVesselDateCompleted(object sender, ServiceReference_HB.GetVesselDateCompletedEventArgs e)
        {
            ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
            comboxVesselDate.ItemsSource = col;
        }
    }//end class
}
